clear all

 
set more off
use "data/dataInsects.dta"


// Data management
if 1 {

	// drop exploratory Stanford sessions
	drop if batch == 2
	
	// drop lines that contain no observations
	keep if species <= 5
	
	
	**** generate variables
	if 1 {
		
		gen topAnte = vAnte == 60
		bysort id: egen topTypeManyAnte = max(topAnte)
		bysort id: egen topTypeFewAnte = min(topAnte)
		
		gen topPost = vPost == 60
		bysort id: egen topTypeManyPost = max(topPost)
		bysort id: egen topTypeFewPost = min(topPost)
		
		gen topLowAnte = vAnte > 30
		bysort id: egen topTypeLowFewAnte = min(topLowAnte)
		
		bysort id: egen topTypeFewBin = max(binarydec) // for binarydec 0 is reject, 1 is accept
		replace topTypeFewBin = 1 - topTypeFewBin
		
		gen bottomAnte = vAnte == 0
		bysort id: egen bottomTypeManyAnte = max(bottomAnte)
		bysort id: egen bottomTypeFewAnte = min(bottomAnte)
		
		bysort id: egen bottomTypeFewBin = min(binarydec) // for binarydec 0 is reject, 1 is accept
		replace bottomTypeFewBin = 1 - bottomTypeFewBin
		
		gen bottomPost = vPost == 0
		bysort id: egen bottomTypeManyPost = max(bottomPost)
		bysort id: egen bottomTypeFewPost = min(bottomPost)
		
		gen untreatableTop = topTypeFewAnte == 1 & topTypeFewPost == 1
		gen untreatableBottom = bottomTypeFewAnte == 1 & bottomTypeFewPost == 1
		gen treatableTop = 1 - untreatableTop
		gen treatableBottom = 1 - untreatableBottom

		gen treatable = (topTypeFewAnte == 0 | topTypeFewPost == 0) & (bottomTypeFewAnte == 0 | bottomTypeFewPost == 0)
		
		capture drop treatment
		capture gen treatment = 1 - control
		capture gen interTrInc = treatment * highInc
		capture gen vAnteInv = 60 - vAnte

		replace male = 2 if male == .
		replace pet = 0 if pet == .
		
		gen unispecies = 10 * university + species
		
		gen weird = v1 == "R_2atEOcb5bV7Kosd" | v1 == "R_eKWNoQjeBYN07qt" | v1 == "R_7QUmW1PA2M8pzY9" ///
			| v1 == "R_bJlcGjbLrNhIaP3" | v1 == "R_6fHRigdB1fJeDmB" | v1 == "R_9stdxcQ6XetoUi9" 

		replace pet = 0 if pet == .
		
		gen agesq = age^2
	}	
		
	drop if id == . | species == .

	cap gen surplus = .
	replace surplus = 30 - vPost if highInc == 1
	replace surplus = 3  - vPost if highInc == 0
	replace surplus = 0 if binarydec == 0

	cap gen negSurplus = surplus < 0
	replace negSurplus = . if surplus == .
	cap gen highInc_treatment = highInc * treatment
	
	gen bet = binarydec
	
}


// Numbers reported in paper
if 1 {
		
	// Information choice
	if 1 {
		
		// Choice of videos
		cap gen infoStr = videoChoiceTry
		tab highInc if tag == 1, su(infoStr)
		reg infoStr highInc if tag == 1
		lincom _b[_cons] + _b[highInc]
		
		
		// Choice of clips
		
		tab highInc if tag == 1, su(numPro)
		tab highInc if tag == 1, su(numCon)
		
		reg numPro highInc if tag == 1
		nlcom _b[highInc]/_b[_cons]
		
		reg numCon highInc if tag == 1
		nlcom 1 - (_b[_cons] + _b[highInc])/_b[_cons]
		
		reg numOth highInc if tag == 1
		lincom _b[_cons] + _b[highInc]
		
		
		gen diffProCon = numPro - numCon
		reg diffProCon highInc if tag == 1
		su diffProCon if highInc == 0
		su diffProCon if highInc == 1
		

	}
	
	// Supply 
	if 1 {
		cap gen bet = binarydec
		// Video condition
		tab highInc if treatment == 1, su(bet)
		// No Video condition
		tab highInc if treatment == 0, su(bet)
		
		reg bet highInc if treatment == 0 , cl(id)
		reg bet highInc if treatment == 1 , cl(id)

		
		reg bet highInc treatment highInc_treatment, cl(id)
		test highInc_treatment
		
	}
	
	// Change in reservation prices
	if 1 {
		
		preserve
			cap gen diffV = vPost - vAnte
			
			reg diffV if species == 1
			reg diffV if species == 2
			reg diffV if species == 3
			reg diffV if species == 4
			reg diffV if species == 5
			
			cap gen absDiffV = abs(diffV)
			cap drop meanAbsDiffV
			bysort id: egen meanAbsDiffV = mean(absDiffV)
			reg meanAbsDiffV, cl(id)
		restore
	}
	
	// Subjects by university
	if 1 {
		la de uni 1 "Stanford" 2 "OSU" 3 "Michigan"
		la val university uni

		tab university if tag == 1
	}
	
	// Demographic summary statistics
	if 1{
		by id: tab male	
		su age
	}
	
}


// Tables 
if 1 {
	
	// Supplementary Table A.1: Summary statistics 
	if 1 {
		***** Counting Subjects
		
		// Stanford
		count if (univ == 1 | univ == 3) & tag == 1
			/* count if session <= 3 & tag == 1
			count if univ == 1 & tag == 1
			count if univ == 3 & tag == 1
			*/
		// OSU
		count if univ == 2 & tag == 1
		// UMich
		count if univ == 4 & tag == 1
		
		
		count if treatment == 0 & tag == 1 
		count if treatment == 1 & tag == 1 
		count if highInc == 0 & treatment == 0 & tag == 1 
		count if highInc == 1 & treatment == 0 & tag == 1 
		count if highInc == 0 & treatment == 1 & tag == 1 
		count if highInc == 1 & treatment == 1 & tag == 1 
		
		
		***** Randomization Check
		
		local varlist = "male age afram caucasian eastAsian hispanic indian ethnOther spending yearofstud graduate artsHumanities busEc engineering science socScience political iq crtS insectBefore cultureInEntomophagy westernFood pet knowBefore"
		postfile randCheck str60 var double highIncInfo lowIncInfo highIncNoInfo lowIncNoInfo p str4 code  using "data/randCheckAll.tex", replace	
		foreach var in `varlist' {
			su `var' if highInc == 1 & control == 0
			local p1 = round(r(mean), 0.01)
			su `var' if highInc == 0 & control == 0
			local p2 = round(r(mean), 0.01)
			su `var' if highInc == 1 & control == 1
			local p3 = round(r(mean), 0.01)
			su `var' if highInc == 0 & control == 1
			local p4 = round(r(mean), 0.01)
			xi: reg `var' i.treatIndex if tag == 1
			local pF = round(Ftail(e(df_m), e(N) - 1, e(F)), 0.001)
		
			local lab: variable label `var'
			post randCheck ("`lab'") (`p1') (`p2') (`p3') (`p4') (`pF') ("\\")
		}
		
		postclose randCheck
		
		preserve 
		clear
		use "data/randCheckAll.tex"
		format %9.2fc highIncInfo lowIncInfo highIncNoInfo lowIncNoInfo
		format %9.3fc p
		outsheet using "../writeup/tables/SupplementaryTableA1.tex", delimiter("&") nonames replace
		restore
		
		***** Subjects who buy out
		
		count if completion == 2 & tag == 1
		local buyOut = r(N)
		count if tag == 1
		local total = r(N)
		di `buyOut' / `total'
		
		su videoChoiceTry if completion == 2
		su clips if completion == 2
		
		***** Inconsistencies
		
		gen vAnteTooHigh = 0
		gen vAnteTooLow = 0
		replace vAnteTooHigh = 1 if binarydec == 1 & vAnte > 3 & highInc == 0
		replace vAnteTooLow  = 1 if binarydec == 0 & vAnte < 3 & highInc == 0
		replace vAnteTooHigh = 1 if binarydec == 1 & vAnte > 30 & highInc == 1
		replace vAnteTooLow  = 1 if binarydec == 0 & vAnte < 30 & highInc == 1
		
		gen inconsistent = vAnteTooHigh + vAnteTooLow
		bysort id: egen inconsSubject = max(inconsistent)
		
		su inconsistent inconsSubject
		tab highInc if control == 0, su(vAnteTooHigh)
		tab highInc if control == 0, su(vAnteTooLow)
		tab highInc if control == 1, su(vAnteTooHigh)
		tab highInc if control == 1, su(vAnteTooLow)

		tab highInc if control == 0, su(inconsistent)
		tab highInc if control == 1, su(inconsistent)
		
		su inconsistent if control == 0
		su inconsistent if control == 1
		reg inconsistent control highInc interaction, cluster(id)
			
		
		
		***** Summary Stats on WTA
		

		postfile manipCheck str60 species double anteMedian fracRepulsed fracTop untreatable str4 code  using "data/manipCheckAll.dta", replace
		
		preserve 
			forvalues species = 1 / 5 {
			
				su vAnte if species == `species', d
				local pM = round(r(p50), 0.01)
				su repulsive if species == `species'
				local p2 = round(r(mean), 0.01)
				su topTrunc if species == `species'
				local p3 = round(r(mean), 0.01)
				su untreatableTop
				local untreatable = round(r(mean), 0.01) 
				local lab : label species `species'
				post manipCheck ("`lab'") (`pM') (`p2') (`p3') (`untreatable') ("\\")
			}
		restore
		
		postclose manipCheck
		
		preserve 
			clear
			use "data/manipCheckAll.dta"
			order fracRepulsed anteMedian  fracTop untreatable
			format %9.2fc anteMedian fracRepulsed fracTop untreatable
			outsheet using "../writeup/tables/manipCheckAll.tex", delimiter("&") nonames replace
		restore

		
		
		count if topTypeFewAnte == 1 & topTypeFewPost == 1 & tag == 1
		local topOut = r(N)
		count if tag == 1
		local total = r(N)
		di `topOut' / `total'
		
		
		areg vAnte i.ethni, a(unispec) cl(id)
		
		areg diff if treatableTop == 1, a(unispec) cl(id)
		
		
		
		
		
		
		****** untreatable participants

		capture: gen t1 = treatInd == 1
		capture: gen t2 = treatInd == 2
		capture: gen t3 = treatInd == 3
		capture: gen t4 = treatInd == 4
		
		reg untreatableTop t1 t2 t3 if tag == 1
		reg untreatableTop t1 t2 t3 t4 if tag == 1, nocons
		test _b[t1] == _b[t2]
		test _b[t1] == _b[t3]
		test _b[t1] == _b[t4]
		test _b[t2] == _b[t3]
		test _b[t1] == _b[t4]
		test _b[t3] == _b[t4]
		
		reg untreatableBottom t1 t2 t3 if tag == 1	
		reg untreatableBottom t1 t2 t3 t4 if tag == 1, nocons
		test _b[t1] == _b[t2]
		test _b[t1] == _b[t3]
		test _b[t1] == _b[t4]
		test _b[t2] == _b[t3]
		test _b[t1] == _b[t4]
		test _b[t3] == _b[t4]
		
		tab treatInd untreatableTop if tag == 1, chi2
		tab treatInd untreatableTop if control == 0 & tag == 1, chi2
		tab treatInd untreatableTop if control == 1 & tag == 1, chi2
		
		

		
		*** sessions

		gen double dateNum = date(startDate, "MD20Y#")
		replace dateNum = date(startDate, "YMD#") if dateNum == .

		preserve
			keep if tag == 1
			replace session = session - 6 if session >= 10
			collapse univ control dateNum (count) id, by(session)		
			format dateNum %tdnn/dd/ccyy
			label de control 0 "video" 1 "no video"
			label val control control	
			order univ session dateNum control 
		restore

	}

	// Supplementary Table A.2: Change in reservation price
	// and 
	// Supplementary Table A.3: Effects of ethnicity
	if 1 {
		
		cap gen diffV = vPost - vAnte
		
		cap gen absDiffV = abs(diffV)
		su absDiffV, d
		cap drop minAbsDiffV
		cap drop meanAbsDiffV
		bysort id: egen minAbsDiffV = min(absDiffV)
		bysort id: egen meanAbsDiffV = mean(absDiffV)
		su minAbsDiffV
		su meanAbsDiffV, d
		su absDiffV 
		
		
		reg absDiffV if species == 1
		reg absDiffV if species == 2
		reg absDiffV if species == 3
		reg absDiffV if species == 4
		reg absDiffV if species == 5

		su vAnte 
		su vAnte if species == 1
		su vAnte if species == 2
		su vAnte if species == 3
		su vAnte if species == 4
		su vAnte if species == 5

		su vPost 
		su vPost if species == 1
		su vPost if species == 2
		su vPost if species == 3
		su vPost if species == 4
		su vPost if species == 5

		cap gen highInc_treatment = highInc * treatment
		
		// Reservation price and demographics (Supplementary Table A.3)
		if 1 {
			
			reg vAnte `predictors', cl(id)
			reg vPost `predictors', cl(id)
			
			replace lowerVante = . if vAnte == .
			replace upperVante = . if vAnte == .
			replace lowerVpost = . if vPost == .
			replace upperVpost = . if vPost == .
			
			local predictors = "afram eastAsian hispanic indian ethnOther"
			reg binarydec `predictors' highInc, cl(id)
			local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA3.tex", ///
			addstat("subj", `subj') adec(3) dec(3) tex(frag) replace
			intreg lowerVante upperVante `predictors', cl(id)
			outreg2 using "../writeup/tables/SupplementaryTableA3.tex", ///
			addstat("subj", `subj') adec(3) dec(3) tex(frag) 
			intreg lowerVpost upperVpost `predictors', cl(id)
			outreg2 using "../writeup/tables/SupplementaryTableA3.tex", ///
			addstat("subj", `subj') adec(3) dec(3) tex(frag) 
			}
		
		// Reservation prices (Supplementary Table A.2)
		if 1 {
			
			preserve	
			replace species = . if vAnte == .
			
			cap drop v1
			cap drop v0
			gen v1 = vPost
			gen v0 = vAnte
			rename upperVpost upperV1
			rename lowerVpost lowerV1
			rename upperVante upperV0
			rename lowerVante lowerV0
			
			local controls = "highInc treatment highInc_treatment "

			
			keep upperV1 lowerV1 upperV0 lowerV0 v0 v1 id species `controls'
			reshape long upperV lowerV v, i(id species) j(after)
			
			local interaction = ""
			foreach var in `controls' {
				cap gen after_`var' = after * `var'
				local interaction = "`interaction'" + " after_`var'"
			}
			
			
			
			////////////
			
			reg v after if species ~= . , cl(id)
				lincom _cons + after
				local vAnteMean = r(estimate)
				local vAnteSE = r(se)
				local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA2.tex", ///
			addstat("subj", `subj', "vAnteMean", `vAnteMean', "vAnteSE", `vAnteSE') adec(3) dec(3) tex(frag) replace
			
			reg v after if species == 1, cl(id)
				lincom _cons + after
				local vAnteMean = r(estimate)
				local vAnteSE = r(se)
				local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA2.tex", ///
			addstat("subj", `subj', "vAnteMean", `vAnteMean', "vAnteSE", `vAnteSE') adec(3) dec(3) tex(frag)
				
			reg v after if species == 2, cl(id)
				lincom _cons + after
				local vAnteMean = r(estimate)
				local vAnteSE = r(se)
				local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA2.tex", ///
			addstat("subj", `subj', "vAnteMean", `vAnteMean', "vAnteSE", `vAnteSE') adec(3) dec(3) tex(frag)
			
			reg v after if species == 3, cl(id)
				lincom _cons + after
				local vAnteMean = r(estimate)
				local vAnteSE = r(se)
				local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA2.tex", ///
			addstat("subj", `subj', "vAnteMean", `vAnteMean', "vAnteSE", `vAnteSE') adec(3) dec(3) tex(frag)

			reg v after if species == 4, cl(id)
				lincom _cons + after
				local vAnteMean = r(estimate)
				local vAnteSE = r(se)
				local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA2.tex", ///
			addstat("subj", `subj', "vAnteMean", `vAnteMean', "vAnteSE", `vAnteSE') adec(3) dec(3) tex(frag)

			reg v after if species == 5, cl(id)
				lincom _cons + after
				local vAnteMean = r(estimate)
				local vAnteSE = r(se)
				local subj = e(N_clust)
			outreg2 using "../writeup/tables/SupplementaryTableA2.tex", ///
			addstat("subj", `subj', "vAnteMean", `vAnteMean', "vAnteSE", `vAnteSE') adec(3) dec(3) tex(frag)
			
		restore
		}
		
		

	}

	// Supplementary Table A.4.A: Welfare effects by species
	if 1 {
		
		cap drop win gain loss
		cap gen win = . 
		replace win = 30 if highInc == 1 
		replace win = 3 if highInc == 0
		cap drop betCE
		cap gen betCE = binarydec * surplus
		cap gen gain = .
		cap gen loss = . 
		replace gain = max(0, betCE)
		replace loss = min(0, betCE)
		cap drop mid
		cap gen mid = (gain + loss)/2
		
		cap gen winMin = win - 1
		replace winMin = . if win == 0
		
		gen condition = species + 5*control

		mysureg (gain win) /// 
				(loss win) if treatment == 1 ///
			, vce(cluster id)
			
		// Minimal required psych benefit
		nlcom  [loss]_cons * ( [gain]win / [loss]win ) - [gain]_cons
			// No such number exists, because losses do not get worse with the incentive
			

		mysureg (gain win) /// 
				(loss win) if treatment == 0 ///
			, vce(cluster id)
			
		// Minimal required psych benefit
		nlcom  [loss]_cons * ( [gain]win / [loss]win ) - [gain]_cons
			// The estimate of the intercept term for gains would need to be higher 
			// by USD 67.70
			
					

		cap postclose welfareTable
		postfile welfareTable ///
				 aG str3 starsGainA double aL str3 starsLossA ///
				 bG str3 starsGain double bL str3 starsLoss ///
				 double alphaMin str4 starsAlpha ///
				 double alphaMax str4 starsAlphaMax ///
				 str4 cond double p ///
				 using "data/welfareTable.dta", replace
				 
		foreach species of numlist 1/10 {
			preserve 
				
				keep if condition == `species'

					
				// Account for censoring in vPost
				if 1 {
					
					// Assume vPost is lognormally distributed. Then estimate the 
					// conditional means using Tobit. Replace observations of vPost 
					// that are censored above with the estimated mean conditional 
					// on being above the threshold.
					
					gen logVpost = ln(vPost)
										
					su vPost, d
					local med = r(p50)
					su logVpost if vPost > `med'
					// local min = r(min)
					local max = r(max)
					
					tobit logVpost if bet == 1 & highInc == 1, ul(`max') 
					
					mat B = e(b)
					
					local mu = B[1,1]
					local sigma = sqrt(B[1,2])
					local alpha = (`max' - `mu')/`sigma'
					
					
					local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
					noisily di exp(`condMean')
					
					replace logVpost = `condMean' if logVpost == `max' & highInc == 1
					
					
					
					tobit logVpost if bet == 1 & highInc == 0, ul(`max') 
					
					mat B = e(b)
					
					local mu = B[1,1]
					local sigma = sqrt(B[1,2])
					local alpha = (`max' - `mu')/`sigma'
					
					
					local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
					noisily di exp(`condMean')
					
					replace logVpost = `condMean' if logVpost == `max' & highInc == 0
					
					replace vPost = exp(logVpost)
					replace vPost`benchmarkCase' = vPost
					
				}
					
				replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
				replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
				replace surplus = 0 if binarydec == 0
			
				cap drop betCE
				cap drop gain
				cap drop loss
				cap drop betCE
				gen betCE = binarydec * surplus
				cap gen gain = .
				cap gen loss = . 
				replace gain = max(0, betCE)
				replace loss = min(0, betCE)

				
				mysureg (gain win) /// 
						(loss win) ///
						, vce(cluster id)
			
				
				local mMin = 3
				local mMax = 30
				
				// Condition

				nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
							- ([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win))) ///
							(eq2: (([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win)))) ///	
							(eq3: ([gain]_cons + `mMin'*[gain]win)) ///
							(eq4: ([loss]_cons + `mMin'*[loss]win)) ///
							(eq5: (([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win)))) ///
							(eq6: ([gain]_cons + `mMax'*[gain]win)) ///
							(eq7: ([loss]_cons + `mMax'*[loss]win)) ///
						, post
				
				
				local bG = _b[eq6]
				local bL = _b[eq7]
				local seBg = _se[eq6] 
				local seBl = _se[eq7]
				
				test eq6 == 0
				local pGain = r(p)
				test eq7 == 0
				local pLoss = r(p)
				
				
				local starsGain = ""
				if (`pGain' <= 0.01) {
					local starsGain = "***"
				}
				if (`pGain' <= 0.05 & `pGain'>0.01) {
					local starsGain = "**"
				}
				if (`pGain' < 0.1 & `pGain'>0.05) {
					local starsGain = "*"
				}
				
				
				local starsLoss = ""
				if (`pLoss' <= 0.01) {
					local starsLoss = "***"
				}
				if (`pLoss' <= 0.05 & `pLoss'>0.01) {
					local starsLoss = "**"
				}
				if (`pLoss' < 0.1 & `pLoss'>0.05) {
					local starsLoss = "*"
				}
				
				
				
				
				// Previous
				
				
				local alphaMin = _b[eq2]
				local alphaSE = _se[eq2]
				local alphaMax = _b[eq5]
				local alphaMaxSE = _se[eq5]
				
				test eq2 == 0
				local pAlpha = r(p)
				
				local starsAlpha = ""
				if (`pAlpha' <= 0.01) {
					local starsAlpha = "***"
				}
				if (`pAlpha' <= 0.05 & `pAlpha'>0.01) {
					local starsAlpha = "**"
				}
				if (`pAlpha' < 0.1 & `pAlpha'>0.05) {
					local starsAlpha = "*"
				}

				local possibilityCond = "No"
				if (_b[eq1] >= 0) {
					local possibilityCond = "Yes"
				}
				
				
				test eq5 == 1
				local pAlphaMax = r(p)
				
				local starsAlphaMax = ""
				if (`pAlphaMax' <= 0.01) {
					local starsAlphaMax = "***"
				}
				if (`pAlphaMax' <= 0.05 & `pAlpha'>0.01) {
					local starsAlphaMax = "**"
				}
				if (`pAlphaMax' < 0.1 & `pAlpha'>0.05) {
					local starsAlphaMax = "*"
				}

				local possibilityCond = "No"
				if (_b[eq1] >= 0) {
					local possibilityCond = "Yes"
				}
				
				
				local aG = _b[eq3]
				local aGse = _se[eq3]
				local aL = _b[eq4]
				local aLse = _se[eq4]
				
				
				
				test _b[eq3] == 0
				local pGainA = r(p)
				test _b[eq4] == 0
				local pLossA = r(p)
				
				
				local starsGainA = ""
				if (`pGainA' <= 0.01) {
					local starsGainA = "***"
				}
				if (`pGainA' <= 0.05 & `pGainA'>0.01) {
					local starsGainA = "**"
				}
				if (`pGainA' < 0.1 & `pGainA'>0.05) {
					local starsGainA = "*"
				}
				
				
				local starsLossA = ""
				if (`pLossA' <= 0.01) {
					local starsLossA = "***"
				}
				if (`pLossA' <= 0.05 & `pLossA'>0.01) {
					local starsLossA = "**"
				}
				if (`pLossA' < 0.1 & `pLossA'>0.05) {
					local starsLossA = "*"
				}
				
				
				noisily: di "`possibilityCond'"
						
				test eq1
				local pTest = r(p)

				
			restore
			post welfareTable ///
				(`aG') ("`starsGainA'") (`aL') ("`starsLossA'")  ///
				(`bG') ("`starsGain'") (`bL') ("`starsLoss'") ///
				(`alphaMin') ("`starsAlpha'") (`alphaMax') ("`starsAlphaMax'") ///
				("`possibilityCond'") (`pTest')
							
			post welfareTable ///
				(`aGse') ("") (`aLse') ("") ///
				(`seBg') ("") (`seBl') ("") ///
				(`alphaSE') ("") (`alphaMaxSE') ("") ("") (.)
			

			
		}
		postclose welfareTable
		
		// Format the table (Supplementary Table A.4.A)
		preserve
			clear
			use "data/welfareTable.dta"
							
			gen c1 = "("
			replace c1 = "" if mod(_n,2) == 1
			
			gen c2 = ") & ("
			replace c2 = "  &  " if mod(_n,2) == 1
			
			gen c3 = c2
			gen c4 = c2
			gen c5 = c2
			gen c6 = c2
			
			gen c7 = ") & "
			replace c7 = "  &  " if mod(_n,2) == 1
			
			gen c8 = " & " 
			
			gen c9 = " \\ "
			
			order c1 aG starsGainA c2 ///
					 aL starsLossA c3 ///
					 alphaMax starsAlphaMax c6 ///
					 bG starsGain  c4 ///
					 bL starsLoss  c5 /// 
					 alphaMin starsAlpha c7 ///
					 cond c8 p c9
			
			
			// Formatting the table
			
			/*
			Condition
			2 house crickets
			5 superworms
			3 silkworm pupae
			2 mole crickets
			2 field crickets
			*/
			
			gen rowlabel = "&"
			replace rowlabel = "2 house crickets	&" 	if mod(_n, 10) == 1 
			replace rowlabel = "5 superworms	&" 		if mod(_n, 10) == 3
			replace rowlabel = "3 silkworm pupae	&" 	if mod(_n, 10) == 5
			replace rowlabel = "2 mole crickets	&" 	if mod(_n, 10) == 7
			replace rowlabel = "2 field crickets	&" 	if mod(_n, 10) == 9
			
			local lines = _N
			local newLines = `lines' + 2
			di `newLines'
			set obs `newLines'
			
			replace rowlabel = "\textit{Video} \\" 		if _n == 21
			replace rowlabel = "\textit{No Video} \\" 	if _n == 22
			
			gen location = _n
			replace location = 0 		if location == 21
			replace location = 10.5 	if location == 22
			
			drop if location > 10
			drop if location == 0

			sort location
			drop location
			order rowlabel
			
			
			format aG aL bG bL alphaMin alphaMax p %9.3f
			export delimited using "../writeup/tables/SupplementaryTableA4A.tsv", delimiter(tab)  datafmt novar replace
							
		restore
		
		// Format the table: No Video condition
		preserve
			clear
			use "data/welfareTable.dta"
			
			replace p = p/2
			
			gen c1 = "("
			replace c1 = "" if mod(_n,2) == 1
			
			gen c2 = ") & ("
			replace c2 = "  &  " if mod(_n,2) == 1
			
			gen c3 = c2
			
			gen c4 = ") & "
			replace c4 = "  &  " if mod(_n,2) == 1
			
			gen c5 = " & " 
			
			gen c6 = " \\ "
			
			order c1 bG starsGain c2 bL starsLoss c3 alphaMin starsAlpha c4 cond c5 p c6
			
			
			// Formatting the table
			
			/*
			Condition
			2 house crickets
			5 superworms
			3 silkworm pupae
			2 mole crickets
			2 field crickets
			*/
			
			gen rowlabel = "&"
			replace rowlabel = "2 house crickets	&" 	if mod(_n, 10) == 1 
			replace rowlabel = "5 superworms	&" 		if mod(_n, 10) == 3
			replace rowlabel = "3 silkworm pupae	&" 	if mod(_n, 10) == 5
			replace rowlabel = "2 mole crickets	&" 	if mod(_n, 10) == 7
			replace rowlabel = "2 field crickets	&" 	if mod(_n, 10) == 9
			
			local lines = _N
			local newLines = `lines' + 2
			di `newLines'
			set obs `newLines'
			
			replace rowlabel = "\textit{Video} \\" 		if _n == 21
			replace rowlabel = "\textit{No Video} \\" 	if _n == 22
			
			gen location = _n
			replace location = 0 		if location == 21
			replace location = 10.5 	if location == 22
			
			keep if location >= 11

			sort location
			drop location
			order rowlabel
			
			
			format bG bL alphaMin p %9.3f
			export delimited using "../writeup/tables/welfareThresholdsLinearNoVideo.tsv", delimiter(tab)  datafmt novar replace
							
		restore
		
		
		
		
	}
	
	
}


// Figures
if 1 {

	// Figure 2.A: Information choice 
	if 1 {
				
		preserve 
			reg videoChoiceTry highInc, cl(id)
				local sdProLow = _se[_cons]
				lincom _cons + highInc
				local sdProHigh = r(se)
		
		
			local aspect = 0.75
			collapse videoChoiceTry , by(highInc)
			gen sdPro = .
			replace sdPro = `sdProLow' if highInc == 0
			replace sdPro = `sdProHigh' if highInc == 1
			
			
			gen upperPro = videoChoiceTry + 1.96*sdPro
			gen lowerPro = videoChoiceTry - 1.96*sdPro
			
			
			// Customize graph scheme
			grstyle init
			grstyle set graphsize 64mm 88mm
			grstyle set margin "15mm 0mm 2.5mm 2.5mm": graph
			grstyle set size 7pt: heading subheading body small_body text_option axis_title tick_label minortick_label key_label
				
			
			tw 	(line videoChoiceTry highInc 		, lcolor(navy) 		lpattern(solid) 	lwidth(0.75)) ///
				(rcap upperPro lowerPro highInc 	, lcolor(navy) 		) ///
				(scatter videoChoiceTry highInc 	, color(navy) 		) ///
				, name(gr1, replace)  ///
				xlabel(0 "$3" 1 "$30") ///
				xtitle("Incentive") ///
				xscale(range(-0.2 1.2)) ///
				ylabel(0.75(0.05)0.95, angle(90)) /// 
				ytitle("Frequency positive video chosen") ///
				legend(off) ///
				bgcolor(white) graphregion(color(white))
			export excel "../writeup/sourceDataFigures/sourceData_Figure2.xlsx", firstrow(variables) sheet("Figure2A_left", replace)

			// Clear customized graph scheme
			grstyle clear
		
		restore
		
		preserve 
			reg numPro highInc, cl(id)
				local sdProLow = _se[_cons]
				lincom _cons + highInc
				local sdProHigh = r(se)
			reg numCon highInc, cl(id)
				local sdConLow = _se[_cons]
				lincom _cons + highInc
				local sdConHigh = r(se)
		
			local aspect = 0.75
			collapse numCon numPro numOth videoChoiceTry, by(highInc)
			gen sdPro = .
			replace sdPro = `sdProLow' if highInc == 0
			replace sdPro = `sdProHigh' if highInc == 1
			
			gen sdCon = .
			replace sdCon = `sdConLow' if highInc == 0
			replace sdCon = `sdConHigh' if highInc == 1
			
			gen upperPro = numPro + 1.96*sdPro
			gen lowerPro = numPro - 1.96*sdPro

			gen upperCon = numCon + 1.96*sdCon
			gen lowerCon = numCon - 1.96*sdCon
			
			// Customize graph scheme
			grstyle init
			grstyle set graphsize 64mm 88mm
			grstyle set margin "0mm 15mm 2.5mm 2.5mm": graph
			grstyle set size 7pt: heading subheading body small_body text_option axis_title tick_label minortick_label key_label
			
			
			tw 	(line numPro highInc 		, lcolor(navy) 		lpattern(solid) 	lwidth(0.75)) ///
				(line numCon highInc 		, lcolor(maroon) 	lpattern(longdash) 	lwidth(0.75)) ///
				(rcap upperPro lowerPro highInc 		, lcolor(navy) 		) ///
				(rcap upperCon lowerCon highInc 		, lcolor(maroon) 	) ///
				(scatter numPro highInc 	, color(navy) 		) ///
				(scatter numCon highInc 	, color(maroon) 	) ///
				, name(gr2, replace)  ///
				xlabel(0 "$3" 1 "$30") ///
				xtitle("Incentive") ///
				ytitle("Number of clips chosen") ///
				xscale(range(-0.2 1.2)) ///
				ylabel(1(0.5)2.5, angle(90)) /// 
				legend(order(1 "Pro" 2 "Con" ) col(1) ring(0) pos(3) region(lc(black))) ///
				bgcolor(white) graphregion(color(white))
			export excel "../writeup/sourceDataFigures/sourceData_Figure2.xlsx", firstrow(variables) sheet("Figure2A_right", modify)
				
			// Clear customized graph scheme
			grstyle clear
		
		restore
		
		
		// Customize graph scheme
		grstyle init
		grstyle set graphsize 64mm 88mm
		grstyle set size 7pt: heading subheading body small_body text_option axis_title tick_label minortick_label key_label
		
		graph combine gr1 gr2, ///
			cols(2) ///
			graphregion(color(white)) ///
			iscale(1)
		graph export "../writeup/graphs/Figure2A.pdf", replace
		
		// Clear customized graph scheme
		grstyle clear
		
	}
		
	// Figure 2.B: Supply curves 
	if 1 {
		preserve

			cap gen lowIncKnown		= highInc == 0 & treatment == 1
			cap gen highIncKnown	= highInc == 1 & treatment == 1
			cap gen lowIncUnknown	= highInc == 0 & treatment == 0
			cap gen highIncUnknown	= highInc == 1 & treatment == 0
			
			reg binarydec lowIncKnown highIncKnown lowIncUnknown highIncUnknown , nocons cl(id)
			local m1 = _b[lowIncKnown]
			local s1 = _se[lowIncKnown]
			local m2 = _b[highIncKnown]
			local s2 = _se[highIncKnown]
			local m3 = _b[lowIncUnknown]
			local s3 = _se[lowIncUnknown]
			local m4 = _b[highIncUnknown]
			local s4 = _se[highIncUnknown]

			
			clear
			set obs 6
			gen mean = .
			gen lower = .
			gen upper = . 
			gen id = _n
			gen highInc = (id == 2 | id == 4)
			gen known = (id == 1 | id == 2) 
			gen zero = 0.3
			local margin = 0.15
			replace highInc = -`margin' if id == 5
			replace highInc = 1 + `margin' if id == 6
			local shift = -0.008
			gen highIncShift = highInc + `shift'
			
			
			forvalues i = 1 / 4 {
				replace mean = `m`i'' if id == `i'
				replace lower = mean - 1.96 * `s`i'' if id == `i'
				replace upper = mean + 1.96 * `s`i'' if id == `i'
			}

			local aspect = 0.75
					
			// Customize graph scheme
			grstyle init
			grstyle set graphsize 64mm 88mm
			grstyle set margin "7.5mm 7.5mm 2.5mm 2.5mm": graph
			grstyle set size 7pt: heading subheading body small_body text_option axis_title tick_label minortick_label key_label	
					
			tw 	(line mean highInc if known == 1, lwidth(0.75) lcolor(navy)) ///
				(line mean highInc if known == 0, lwidth(0.75) lpattern(dash) lcolor(maroon)) ///
				(scatter mean highInc if known == 1, msize(vlarge) mcolor(navy)) ///	
				(scatter mean highInc if known == 0, msize(vlarge) mcolor(maroon)) ///	
				(rcap upper lower highInc if known == 1, lcolor(navy)) ///
				(rcap upper lower highInc if known == 0, lcolor(maroon)) ///			
				(line zero highInc if highInc < 0) ///
				, ///
				xlabel(0 "$3" 1 "$30") xtitle("Incentive") xscale(range(-0.2 1.2)) ///
				legend(order(1 "Video condition" 2 "No Video condition" )  ring(0) col(1) pos(11) region(lc(black))) /// 
				ylabel(0.3(0.1)0.8, angle(90)) /// 
				ytitle("Participation frequency") ///
				aspect(`aspect') ///
				bgcolor(white) graphregion(color(white)) 
			graph export "../writeup/graphs/Figure2B.pdf", replace
			export excel "../writeup/sourceDataFigures/sourceData_Figure2.xlsx", firstrow(variables) sheet("Figure2B", modify)
			
			// Clear customized graph scheme
			grstyle clear
			
		restore
	
	}
		
	// Figure 2.C: Producer surplus graph known
	if 1 {
		preserve
			
			cap gen consSurplus = .
			replace consSurplus = 30 - vPost if highInc == 1
			replace consSurplus = 3  - vPost if highInc == 0
			replace consSurplus = 0 if binarydec == 0
			
			gen censored = vPost == 60
				
			cap gen known = 1 - control
			
			cap cumul consSurplus if known == 1 & highInc == 1, gen(diffHighTr)
			cap cumul consSurplus if known == 1 & highInc == 0, gen(diffLowTr)
			
		
			// Plot the part of the CDF corresponding to the censored observations
			
			gen censoredHighLeft = -30 
			gen censoredHighRight = -17
			gen surplusHighCensored = censoredHighLeft
			replace surplusHighCensored = censoredHighRight if runiform() < 0.5
			
			gen censoredLowLeft = -60 + 3 
			gen censoredLowRight = -47 + 3
			gen surplusLowCensored = censoredLowLeft
			replace surplusLowCensored = censoredLowRight if runiform() < 0.5
							
			su diffHighTr if surplus < -20 & highInc == 1
			local max = r(max)
			gen diffHighCensored = `max' if censored == 1
							
			su diffLowTr if surplus < -55 & highInc == 0
			local max = r(max)
			gen diffLowCensored = `max' if censored == 1
			
			// Customize graph scheme
			grstyle init
			grstyle set graphsize 64mm 88mm
			grstyle set margin "7.5mm 7.5mm 2.5mm 2.5mm": graph
			grstyle set size 7pt: heading subheading body small_body text_option axis_title tick_label minortick_label key_label
			
			sort diffHighTr diffLowTr 
			tw	(line diffHighTr consSurplus if censored == 0, lwidth(0.75) lcolor(navy)   lpattern(solid))	 ///
				(line diffLowTr  consSurplus if censored == 0, lwidth(0.75) lcolor(maroon) lpattern(longdash)) 	///
				(line diffHighCensored surplusHighCensored if censored == 1, lwidth(0.75) lcolor(navy)   lpattern(solid))	 ///
				(line diffLowCensored  surplusLowCensored  if censored == 1, lwidth(0.75) lcolor(maroon) lpattern(longdash)) 	///
				, ///
				xtitle("Ex-post surplus in $") ///
				legend(order(1 "Incentive $30" 2 "Incentive $3") ring(0) col(1) pos(10) region(lc(black))) ///		
				ylabel(, angle(90)) ///
				ytitle("Cumulative frequency") ///
				xline(0, lcolor(black) lpattern(solid) lwidth(0.35)) ///
				xline(3, lcolor(black) lpattern(solid) lwidth(0.15)) ///
				xline(30, lcolor(black) lpattern(solid) lwidth(0.15)) ///
				xlabel(-40 -20 0 3 20 30) /// 
				bgcolor(white) graphregion(color(white))
			graph export "../writeup/graphs/Figure2C.pdf", replace
			keep diffHighTr diffLowTr diffHighCensored diffLowCensored consSurplus surplusHighCensored surplusLowCensored censored
			export excel "../writeup/sourceDataFigures/sourceData_Figure2.xlsx", firstrow(variables) sheet("Figure2C", modify)

			// Clear customized graph scheme
			grstyle clear
			
		restore
	}
	
	// Figure 2.D: Effect of anchoring and information biasing: Alternative benchmarks
	// and
	// Supplementary Table A.4.C
	if 1 { 
		
		// Customize graph scheme
		grstyle init
		grstyle set graphsize 64mm 88mm
		grstyle set margin "7.5mm 7.5mm 2.5mm 2.5mm": graph
		grstyle set size 7pt: heading subheading body small_body text_option axis_title tick_label minortick_label key_label

		
		// Numbers mentioned in text
		
		su vPost if treatment == 0 & highInc == 0
		su vPost if treatment == 0 & highInc == 1
		su vPost if treatment == 1 & highInc == 0
		su vPost if treatment == 1 & highInc == 1


		gen condNr = .
		replace condNr = 1 if treatment == 0 & highInc == 0
		replace condNr = 2 if treatment == 1 & highInc == 0
		replace condNr = 3 if treatment == 0 & highInc == 1
		replace condNr = 4 if treatment == 1 & highInc == 1
		
		// Step 1: Match the data 
		if 1 {
		
			// By species: Match by WTA rank
			// Issue: some people might have revealed the WTA level, 
			//		but might get matched to different WTA levels, 
			//		and some might have accepted while others rejected.
			// 		Hence, consider worst-case and best-case: sort by WTA and acceptance, 
			// 		and also sort by WTA and rejection for a second match
			
			gen noBet = 1 - bet	
				
			sort treatment highInc species vPost bet
			order treatment highInc species vPost bet
			
			by treatment highInc species: egen rank = rank(vPost), unique
			
			// Map WTA for condNr = 1 to all other condNr
			
			forvalues i = 1 / 4 {
				su rank if condNr == `i'
				local max`i' = r(max)
			}
			
			// make the ranks for all other condNr conform to those for condNr == i
			forvalues i = 1 / 4 {
				gen rank`i' = .
				forvalues j = 1 / 4 { 
					replace rank`i' = round((`max`i''/`max`j'')*rank, 1) if condNr == `j'
				}
			}
			
			// Copy the WTA info for condNr i to condNr j
			forvalues i = 1 / 4 {
				preserve
					keep if condNr == `i'
					keep rank`i' vPost species
					drop if vPost == . | species == . | rank`i' == . 
					rename vPost vPost`i'
					save "data/wtaRank`i'.dta", replace
				restore
			}
			
			forvalues i = 1 / 4 {
				merge m:1 rank`i' species using "data/wtaRank`i'.dta"
				drop _m
			}
			
		}

		// Step 2: Analysis 
		if 1 {
		
		
			forvalues benchmarkCase = 1 / 4 {
					
						
				preserve
					
					cap drop surplus
					cap gen surplus = .
					
					keep if treatment == 1
						
						
					replace vPost = vPost`benchmarkCase'
					gen censored = vPost == 60
					
					// Account for censoring in vPost
					if 1 {
						
						// Assume vPost is lognormally distributed. Then estimate the 
						// conditional means using Tobit. Replace observations of vPost 
						// that are censored above with the estimated mean conditional 
						// on being above the threshold.
						
						gen logVpost = ln(vPost)
											
						su vPost, d
						local med = r(p50)
						su logVpost if vPost > `med'
						// local min = r(min)
						local max = r(max)
						
						
						
						tobit logVpost if bet == 1 & highInc == 1, ul(`max') 
						
						mat B = e(b)
						
						local mu = B[1,1]
						local sigma = sqrt(B[1,2])
						local alpha = (`max' - `mu')/`sigma'
						
						gen logVpostPred = rnormal(`mu', `sigma')
						// distplot logVpost logVpostPred if bet == 1 & highInc == 1
						// graph export "../writeup/graphs/surplusTobitFitHighInc`benchmarkCase'.pdf", replace				
						
						local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
						noisily di exp(`condMean')
						
						replace logVpost = `condMean' if logVpost == `max' & highInc == 1
						
						
						
						
						tobit logVpost if bet == 1 & highInc == 0, ul(`max') 
						
						mat B = e(b)
						
						local mu = B[1,1]
						local sigma = sqrt(B[1,2])
						local alpha = (`max' - `mu')/`sigma'
						
						cap drop logVpostPred
						gen logVpostPred = rnormal(`mu', `sigma')
						// distplot logVpost logVpostPred if bet == 1 & highInc == 0
						// graph export "../writeup/graphs/surplusTobitFitHighInc`benchmarkCase'.pdf", replace				

						local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
						noisily di exp(`condMean')
						
						replace logVpost = `condMean' if logVpost == `max' & highInc == 0
						
						
						
						
						
						replace vPost = exp(logVpost)
						replace vPost`benchmarkCase' = vPost
						
					}
				
									
					// replace vPost`benchmarkCase' = 100			 if censored == 1
					replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
					replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
					replace surplus = 0 if binarydec == 0
					
				
					cap gen win = . 
					replace win = 30 if highInc == 1 
					replace win = 3 if highInc == 0
					cap drop betCE
					gen betCE = binarydec * surplus
					cap gen gain = .
					cap gen loss = . 
					replace gain = max(0, betCE)
					replace loss = min(0, betCE)
					cap drop mid
					gen mid = (gain + loss)/2

					
					
					
					reg3 	(gain highInc) ///
							(loss highInc)
					
					// Minimal delta for undue inducement condition to hold
					noisily: di "Minimal delta for undue inducement condition to hold"
					noisily: nlcom [loss]_b[_cons] * [gain]_b[highInc] / [loss]_b[highInc] - [gain]_b[_cons]
					
					
					
					
					cumul surplus if highInc == 1, gen(diffHighTr)
					cumul surplus if highInc == 0, gen(diffLowTr)
					
					// Plot the part of the CDF corresponding to the censored observations
					
					gen censoredHighLeft = -30 
					gen censoredHighRight = -17
					gen surplusHighCensored = censoredHighLeft
					replace surplusHighCensored = censoredHighRight if runiform() < 0.5
					
					gen censoredLowLeft = -60 + 3 
					gen censoredLowRight = -47 + 3
					gen surplusLowCensored = censoredLowLeft
					replace surplusLowCensored = censoredLowRight if runiform() < 0.5
									
					su diffHighTr if surplus < -20 & highInc == 1
					local max = r(max)
					gen diffHighCensored = `max' if censored == 1
									
					su diffLowTr if surplus < -55 & highInc == 0
					local max = r(max)
					gen diffLowCensored = `max' if censored == 1
					
					
					
					// Plot the CDFs
			
					sort diffHighTr diffLowTr diffHighCensored diffLowCensored
					tw	(line diffHighTr surplus if censored == 0, lwidth(0.75) lcolor(navy) 	lpattern(solid))	 ///
						(line diffLowTr  surplus if censored == 0, lwidth(0.75) lcolor(maroon) 	lpattern(longdash)) 	///
						(line diffHighCensored surplusHighCensored if censored == 1, lwidth(0.75) lcolor(navy) 	lpattern(solid)) ///
						(line diffLowCensored  surplusLowCensored  if censored == 1, lwidth(0.75) lcolor(maroon) lpattern(longdash)) ///
						, ///
						xtitle("Ex-post surplus in $") ///
						legend(order(1 "Incentive $30" 2 "Incentive $3") ring(0) col(1) pos(10) region(lc(black))) ///		
						ylabel(,angle(90)) ///
						ytitle("Cumulative frequency") ///
						xline(0, lcolor(black) lpattern(solid) lwidth(0.35)) ///
						xline(3, lcolor(black) lpattern(solid) lwidth(0.15)) ///
						xline(30, lcolor(black) lpattern(solid) lwidth(0.15)) ///
						xlabel(-40 -20 0 3 20 30) /// 
						bgcolor(white) graphregion(color(white))
					if `benchmarkCase' == 1 {
						graph export "../writeup/graphs/Figure2D.pdf", replace	
						tempname benchmark1
						save "`benchmark1'", replace
						keep diffHighTr diffLowTr diffHighCensored diffLowCensored surplus surplusHighCensored surplusLowCensored censored 
						export excel "../writeup/sourceDataFigures/sourceData_Figure2.xlsx", firstrow(variables) sheet("Figure2D", modify)
						use "`benchmark1'", clear
					}
					 else {
					 	graph export "../writeup/graphs/producerSurplusBenchmark`benchmarkCase'.pdf", replace	
					}
					 
					

					
					
					
					
					
					
					
					foreach m in 3 30 {
						reg gain if win == `m', cl(id)
						local seG`m' = _se[_cons]
						cap reg loss if win == `m', cl(id)
						local seL`m' = _se[_cons]
						cap reg loss if win == `m', cl(id)
						local seB`m' = _se[_cons]
					}
					
					collapse gain loss mid, by(win)
					
					gen seG = .
					gen seL = .
					gen seB = .
					

					foreach m in 3 30 {
						replace seG = `seG`m''
						replace seL = `seL`m''
						replace seB = `seB`m''
					}
					
					gen upperG = gain + 1.96*seG
					gen lowerG = gain - 1.96*seG
					gen upperL = loss + 1.96*seL
					gen lowerL = loss - 1.96*seL
					gen upperB = mid + 1.96*seB
					gen lowerB = mid - 1.96*seB
					
					
					
					
					tw 	(scatter gain win, mcolor(navy)) ///
						(scatter loss win, mcolor(maroon) msymbol(T)) /// 
						(scatter mid win, mcolor(black) msymbol(D)) /// 
						(lfit gain win, lcolor(navy) lwidth(0.4) lpattern(solid)) ///
						(lfit loss win, lcolor(maroon) lwidth(0.4) lpattern(solid)) /// 
						(lfit mid win, lcolor(black) lwidth(0.4) lpattern(longdash)) /// 
						(rcap lowerG upperG win, lcolor(navy) lwidth(0.15)) ///
						(rcap lowerL upperL win, lcolor(maroon) lwidth(0.15)) ///
						(rcap lowerB upperB win, lcolor(black) lwidth(0.15)) ///
					, /// 
					legend(order(1 "Average gain ({&alpha} = 0)" 3 "Utilitarian welfare  ({&alpha} = 1/2)" 2 "Average loss  ({&alpha} = 1)" ) col(1) ring(0) pos(10) region(lc(black))) ///
					ytitle("USD") ///
					yline(0, lwidth(0.4) lcolor(black) lpattern(shortdash)) ///
					xtitle("Incentive amount in USD")    ///
					bgcolor(white) graphregion(color(white)) ///
					xlabel(3 30) ///
					xscale(range(0 33))		
					graph export "../writeup/graphs/welfareGainLossVideoBenchmark`benchmarkCase'.pdf", replace
					
					
					
					
					
				restore
				
			}
							
				preserve
					
					drop surplus 
					
					cap gen surplus = .
					replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
					replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
					replace surplus = 0 if binarydec == 0
					
				
					cap gen win = . 
					replace win = 30 if highInc == 1 
					replace win = 3 if highInc == 0
					cap drop betCE
					gen betCE = binarydec * surplus
					cap gen gain = .
					cap gen loss = . 
					replace gain = max(0, betCE)
					replace loss = min(0, betCE)
					cap drop mid
					gen mid = (gain + loss)/2

					keep if treatment == 0
					
					foreach m in 3 30 {
						reg gain if win == `m', cl(id)
						local seG`m' = _se[_cons]
						cap reg loss if win == `m', cl(id)
						local seL`m' = _se[_cons]
						cap reg loss if win == `m', cl(id)
						local seB`m' = _se[_cons]
					}
					
					collapse gain loss mid, by(win)
					
					gen seG = .
					gen seL = .
					gen seB = .
					

					foreach m in 3 30 {
						replace seG = `seG`m''
						replace seL = `seL`m''
						replace seB = `seB`m''
					}
					
					gen upperG = gain + 1.96*seG
					gen lowerG = gain - 1.96*seG
					gen upperL = loss + 1.96*seL
					gen lowerL = loss - 1.96*seL
					gen upperB = mid + 1.96*seB
					gen lowerB = mid - 1.96*seB
					
					
					
					
					tw 	(scatter gain win, mcolor(navy)) ///
						(scatter loss win, mcolor(maroon) msymbol(T)) /// 
						(scatter mid win, mcolor(black) msymbol(D)) /// 
						(lfit gain win, lcolor(navy) lwidth(0.4) lpattern(solid)) ///
						(lfit loss win, lcolor(maroon) lwidth(0.4) lpattern(solid)) /// 
						(lfit mid win, lcolor(black) lwidth(0.4) lpattern(longdash)) /// 
						(rcap lowerG upperG win, lcolor(navy) lwidth(0.15)) ///
						(rcap lowerL upperL win, lcolor(maroon) lwidth(0.15)) ///
						(rcap lowerB upperB win, lcolor(black) lwidth(0.15)) ///
					, /// 
					legend(order(1 "Average gain ({&alpha} = 0)" 3 "Utilitarian welfare  ({&alpha} = 1/2)" 2 "Average loss  ({&alpha} = 1)" ) col(1) ring(0) pos(10)  region(lc(black))) ///
					ytitle("USD") ///
					yline(0, lwidth(0.4) lcolor(black) lpattern(shortdash)) ///
					xtitle("Incentive amount in USD")    ///
					bgcolor(white) graphregion(color(white)) ///
					xlabel(3 30) ///
					xscale(range(0 33))		
					graph export "../writeup/graphs/welfareGainLossNoVideoBenchmark`benchmarkCase'.pdf", replace
				restore

				
				
				
			}
		
		// Welfare table No Video, Low Incentive benchmark
		if 1 {
			
			cap drop win gain loss
			
			drop surplus 
			
			local benchmarkCase = 1
			cap gen surplus = .
			replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
			replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
			replace surplus = 0 if binarydec == 0
			
		
			cap gen win = . 
			replace win = 30 if highInc == 1 
			replace win = 3 if highInc == 0
			cap drop betCE
			gen betCE = binarydec * surplus
			cap gen gain = .
			cap gen loss = . 
			replace gain = max(0, betCE)
			replace loss = min(0, betCE)
			cap drop mid
			cap gen mid = (gain + loss)/2
			
			cap drop winMin
			cap gen winMin = win - 1
			replace winMin = . if win == 0
			
			cap drop condition
			gen condition = species + 5*control

			

		

			cap postclose welfareTable
			postfile welfareTable ///
					 aG str3 starsGainA double aL str3 starsLossA ///
					 bG str3 starsGain double bL str3 starsLoss ///
					 double alphaMin str4 starsAlpha ///
					 double alphaMax str4 starsAlphaMax ///
					 str4 cond double p ///
					 using "data/welfareTableAltBenchmark.dta", replace
					 
			foreach species of numlist 1/10 {
				preserve 
					
					keep if condition == `species'
		
					local benchmarkCase = 1
					replace vPost = vPost`benchmarkCase'	
						
					// Account for censoring in vPost
					if 1 {
						
						// Assume vPost is lognormally distributed. Then estimate the 
						// conditional means using Tobit. Replace observations of vPost 
						// that are censored above with the estimated mean conditional 
						// on being above the threshold.
						
						gen logVpost = ln(vPost)
											
						su vPost, d
						local med = r(p50)
						su logVpost if vPost > `med'
						// local min = r(min)
						local max = r(max)
						
						tobit logVpost if bet == 1 & highInc == 1, ul(`max') 
						
						mat B = e(b)
						
						local mu = B[1,1]
						local sigma = sqrt(B[1,2])
						local alpha = (`max' - `mu')/`sigma'
						
						
						local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
						noisily di exp(`condMean')
						
						replace logVpost = `condMean' if logVpost == `max' & highInc == 1
						
						
						
						tobit logVpost if bet == 1 & highInc == 0, ul(`max') 
						
						mat B = e(b)
						
						local mu = B[1,1]
						local sigma = sqrt(B[1,2])
						local alpha = (`max' - `mu')/`sigma'
						
						
						local condMean = `mu'+`sigma'*normalden(`alpha')/(1-normal(`alpha'))
						noisily di exp(`condMean')
						
						replace logVpost = `condMean' if logVpost == `max' & highInc == 0
						
						replace vPost = exp(logVpost)
						replace vPost`benchmarkCase' = vPost
						
					}
						
					local benchmarkCase = 1	
					replace surplus = 30 - vPost`benchmarkCase' if highInc == 1
					replace surplus = 3  - vPost`benchmarkCase' if highInc == 0
					replace surplus = 0 if binarydec == 0
				
					cap drop betCE
					cap drop gain
					cap drop loss
					cap drop betCE
					gen betCE = binarydec * surplus
					cap gen gain = .
					cap gen loss = . 
					replace gain = max(0, betCE)
					replace loss = min(0, betCE)

					
					mysureg (gain win) /// 
							(loss win) ///
							, vce(cluster id)
					
									
					local mMin = 3
					local mMax = 30

					if (`species' == 5) {
						nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
									- ([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win))) ///
								(eq2: (([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win)))) ///	
								(eq3: ([gain]_cons + `mMin'*[gain]win)) ///
								(eq4: ([loss]_cons + `mMin'*[loss]win)) ///
								(eq5: (([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win)))) ///
								(eq6: ([gain]_cons + `mMax'*[gain]win)) ///
								, post
					}
					else {
						nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
									- ([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win))) ///
								(eq2: (([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win)))) ///	
								(eq3: ([gain]_cons + `mMin'*[gain]win)) ///
								(eq4: ([loss]_cons + `mMin'*[loss]win)) ///
								(eq5: (([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win)))) ///
								(eq6: ([gain]_cons + `mMax'*[gain]win)) ///
								(eq7: ([loss]_cons + `mMax'*[loss]win)) ///
								, post
					}
					
					
					noisily: di "`possibilityCond'"
							
					test eq1
					local pTest = r(p)

					
					// New
					
					
					local bG = _b[eq6]
					local seBg = _se[eq6] 
					
					test eq6 == 0
					local pGain = r(p)
					
					
					local starsGain = ""
					if (`pGain' <= 0.01) {
						local starsGain = "***"
					}
					if (`pGain' <= 0.05 & `pGain'>0.01) {
						local starsGain = "**"
					}
					if (`pGain' < 0.1 & `pGain'>0.05) {
						local starsGain = "*"
					}
					
					
					
					
					
					// Previous
					
					
					local alphaMin = _b[eq2]
					local alphaSE = _se[eq2]
					local alphaMax = _b[eq5]
					local alphaMaxSE = _se[eq5]
					
					test eq2 == 0
					local pAlpha = r(p)
					
					local starsAlpha = ""
					if (`pAlpha' <= 0.01) {
						local starsAlpha = "***"
					}
					if (`pAlpha' <= 0.05 & `pAlpha'>0.01) {
						local starsAlpha = "**"
					}
					if (`pAlpha' < 0.1 & `pAlpha'>0.05) {
						local starsAlpha = "*"
					}

					local possibilityCond = "No"
					if (_b[eq1] >= 0) {
						local possibilityCond = "Yes"
					}
					
					
					test eq5 == 1
					local pAlphaMax = r(p)
					
					local starsAlphaMax = ""
					if (`pAlphaMax' <= 0.01) {
						local starsAlphaMax = "***"
					}
					if (`pAlphaMax' <= 0.05 & `pAlpha'>0.01) {
						local starsAlphaMax = "**"
					}
					if (`pAlphaMax' < 0.1 & `pAlpha'>0.05) {
						local starsAlphaMax = "*"
					}

					local possibilityCond = "No"
					if (_b[eq1] >= 0) {
						local possibilityCond = "Yes"
					}
					
					
					local aG = _b[eq3]
					local aGse = _se[eq3]
					local aL = _b[eq4]
					local aLse = _se[eq4]
					
					
					
					test _b[eq3] == 0
					local pGainA = r(p)
					test _b[eq4] == 0
					local pLossA = r(p)
					
					
					if (`species' ~= 5) {
						test eq7 == 0
						local pLoss = r(p)
						local bL = _b[eq7]
						local seBl = _se[eq7]
					}
					else {
						reg loss if win == 30 & species == 5, cl(id)
						local bL = _b[_cons]
						local seBl = _se[_cons]
						local pLoss = 1	
					}
					
					
					local starsLoss = ""
					if (`pLoss' <= 0.01) {
						local starsLoss = "***"
					}
					if (`pLoss' <= 0.05 & `pLoss'>0.01) {
						local starsLoss = "**"
					}
					if (`pLoss' < 0.1 & `pLoss'>0.05) {
						local starsLoss = "*"
					}
					
					
					local starsGainA = ""
					if (`pGainA' <= 0.01) {
						local starsGainA = "***"
					}
					if (`pGainA' <= 0.05 & `pGainA'>0.01) {
						local starsGainA = "**"
					}
					if (`pGainA' < 0.1 & `pGainA'>0.05) {
						local starsGainA = "*"
					}
					
					
					local starsLossA = ""
					if (`pLossA' <= 0.01) {
						local starsLossA = "***"
					}
					if (`pLossA' <= 0.05 & `pLossA'>0.01) {
						local starsLossA = "**"
					}
					if (`pLossA' < 0.1 & `pLossA'>0.05) {
						local starsLossA = "*"
					}
					
					
					
				restore
				post welfareTable ///
					(`aG') ("`starsGainA'") (`aL') ("`starsLossA'")  ///
					(`bG') ("`starsGain'") (`bL') ("`starsLoss'") ///
					(`alphaMin') ("`starsAlpha'") (`alphaMax') ("`starsAlphaMax'") ///
					("`possibilityCond'") (`pTest')
								
				post welfareTable ///
					(`aGse') ("") (`aLse') ("") ///
					(`seBg') ("") (`seBl') ("") ///
					(`alphaSE') ("") (`alphaMaxSE') ("") ("") (.)
				

				
			}
			postclose welfareTable
			
			// Format the table (Supplementary Table A.4.C)
			preserve
				clear
				use "data/welfareTableAltBenchmark.dta"
								
				gen c1 = "("
				replace c1 = "" if mod(_n,2) == 1
				
				gen c2 = ") & ("
				replace c2 = "  &  " if mod(_n,2) == 1
				
				gen c3 = c2
				gen c4 = c2
				gen c5 = c2
				gen c6 = c2
				
				gen c7 = ") & "
				replace c7 = "  &  " if mod(_n,2) == 1
				
				gen c8 = " & " 
				
				gen c9 = " \\ "
				
				order c1 aG starsGainA c2 ///
						 aL starsLossA c3 ///
						 alphaMax starsAlphaMax c6 ///
						 bG starsGain  c4 ///
						 bL starsLoss  c5 /// 
						 alphaMin starsAlpha c7 ///
						 cond c8 p c9
				
				
				// Formatting the table
				
				/*
				Condition
				2 house crickets
				5 superworms
				3 silkworm pupae
				2 mole crickets
				2 field crickets
				*/
				
				gen rowlabel = "&"
				replace rowlabel = "2 house crickets	&" 	if mod(_n, 10) == 1 
				replace rowlabel = "5 superworms	&" 		if mod(_n, 10) == 3
				replace rowlabel = "3 silkworm pupae	&" 	if mod(_n, 10) == 5
				replace rowlabel = "2 mole crickets	&" 	if mod(_n, 10) == 7
				replace rowlabel = "2 field crickets	&" 	if mod(_n, 10) == 9
				
				local lines = _N
				local newLines = `lines' + 2
				di `newLines'
				set obs `newLines'
				
				replace rowlabel = "\textit{Video} \\" 		if _n == 21
				replace rowlabel = "\textit{No Video} \\" 	if _n == 22
				
				gen location = _n
				replace location = 0 		if location == 21
				replace location = 10.5 	if location == 22
				
				drop if location > 10
				drop if location == 0

				sort location
				drop location
				order rowlabel
				
				
				format aG aL bG bL alphaMin alphaMax p %9.3f
				export delimited using "../writeup/tables/SupplementaryTableA4C.tsv", delimiter(tab)  datafmt novar replace
								
			restore
			
			// Format the table: No Video condition
			preserve
				clear
				use "data/welfareTable.dta"
				
				replace p = p/2
				
				gen c1 = "("
				replace c1 = "" if mod(_n,2) == 1
				
				gen c2 = ") & ("
				replace c2 = "  &  " if mod(_n,2) == 1
				
				gen c3 = c2
				
				gen c4 = ") & "
				replace c4 = "  &  " if mod(_n,2) == 1
				
				gen c5 = " & " 
				
				gen c6 = " \\ "
				
				order c1 bG starsGain c2 bL starsLoss c3 alphaMin starsAlpha c4 cond c5 p c6
				
				
				// Formatting the table
				
				/*
				Condition
				2 house crickets
				5 superworms
				3 silkworm pupae
				2 mole crickets
				2 field crickets
				*/
				
				gen rowlabel = "&"
				replace rowlabel = "2 house crickets	&" 	if mod(_n, 10) == 1 
				replace rowlabel = "5 superworms	&" 		if mod(_n, 10) == 3
				replace rowlabel = "3 silkworm pupae	&" 	if mod(_n, 10) == 5
				replace rowlabel = "2 mole crickets	&" 	if mod(_n, 10) == 7
				replace rowlabel = "2 field crickets	&" 	if mod(_n, 10) == 9
				
				local lines = _N
				local newLines = `lines' + 2
				di `newLines'
				set obs `newLines'
				
				replace rowlabel = "\textit{Video} \\" 		if _n == 21
				replace rowlabel = "\textit{No Video} \\" 	if _n == 22
				
				gen location = _n
				replace location = 0 		if location == 21
				replace location = 10.5 	if location == 22
				
				keep if location >= 11

				sort location
				drop location
				order rowlabel
				
				
				format bG bL alphaMin p %9.3f
				export delimited using "../writeup/tables/welfareThresholdsLinearAltBenchmarkNoVideo.tsv", delimiter(tab)  datafmt novar replace
								
			restore
			
			
			
			
		}
		

	// Clear customized graph scheme
	grstyle clear		
		
		
	}	

	
}
































